<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="30%"
  >
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :rules="rules"
      :search-tags="searchTags"
      :select-list="selectList"
      :is-inline="false"/>
    <span slot="footer">
      <el-button
        icon="el-icon-circle-close"
        @click="handleClose"
      >{{ $t("cancel") }}</el-button>
      <el-button
        v-btnThrottle="doSave"
        type="primary"
        icon="el-icon-receiving"
      >{{ $t("save") }}</el-button>
  </span></el-dialog>
</template>

<script>
import { getDetails } from '@/api/checkManagement/checkhistory'
import { getOpportunityNameId } from '@/api/checkManagement/checkTiming'
import { getFormList } from '@/api/eForm/eFormCheck'

export default {
  name: 'CheckTimingBindingDialog',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {},
      selectList: {},
      searchTags: [
        { label: this.$t('checklistName'), prop: 'checklistName', tag: 'DynSelect' },
        { label: this.$t('checkOpportunity'), prop: 'opportunityId', tag: 'DynSelect' }
      ],
      rules: {
        opportunityId: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('checklistName')}`, trigger: 'change' }],
        checklistName: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('checkOpportunity')}`, trigger: 'change' }]
      }
    }
  },
  methods: {
    async handleOpen(row) {
      if (row && row.id) {
        const res = await getDetails({ formId: row.id })
        console.log(res)
        this.title = this.$t('modify')
      } else {
        this.title = this.$t('add')
      }
      this.getOpportunityNames()
      this.getFormCheckList()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    doSave() {
      // this.$refs['dataForm'].validate((valid) => {
      //   if (valid) {
      //     if (this.infoForm.id) {
      //       const filtertable = this.tableData.filter(item => !!item.parmName && (item.lowLimit || item.highLimit))
      //       doUpdate(this.$clearNull({ ...this.infoForm, sblSpecList: filtertable })).then(res => {
      //         this.$betterTips(res, this.handleClose, this.handleUpdate)
      //       })
      //     } else {
      //       const filtertable = this.tableData.filter(item => !!item.parmName && (item.lowLimit || item.highLimit))
      //       doAdd(this.$clearNull({ ...this.infoForm, sblSpecList: filtertable })).then(res => {
      //         this.$betterTips(res, this.handleClose, this.handleUpdate)
      //       })
      //     }
      //   }
      // })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    },
    async getOpportunityNames() {
      const res = await getOpportunityNameId()
      this.$set(this.selectList, 'opportunityId', res.data)
    },
    async getFormCheckList() {
      const res = await getFormList({ category: 2 })
      this.$set(this.selectList, 'checklistName', res.data)
    }
  }
}
</script>
